<template>
	<view style="">
		<!-- <u-navbar :autoBack="true">
			<view class="u-nav-slot" slot="center">
				<view class="" @click="show = true" v-if="show">
					<zb-dropdown-menu :zIndex="89" @toggleItem="toggleItem(2)" ref="dropdown1" active-color="#1cb8a7">
						<zb-dropdown-item :options="option" v-model="value4" @change="change1" name="first"></zb-dropdown-item>
					</zb-dropdown-menu>
				</view>
				<view class="" v-else style="font-weight: 700;font-size: 16px;">
					家长嘱托
				</view>
			</view>
			
		</u-navbar> -->
		<!-- #ifdef MP-WEIXIN -->
		<u-navbar :placeholder="true" :autoBack="true">
			<view class="u-nav-slot" slot="center">
				<view class="" @click="show = true" v-if="show">
					<zb-dropdown-menu :zIndex="89" @toggleItem="toggleItem(2)" ref="dropdown1" active-color="#1cb8a7">
						<zb-dropdown-item :options="option" v-model="value4" @change="change1"
							name="first"></zb-dropdown-item>
					</zb-dropdown-menu>
				</view>
				<view class="" v-else style="font-weight: 700;font-size: 16px;">
					宝宝家属
				</view>
			</view>
			<view class="u-nav-slot" slot="left">
				<u-icon name="arrow-left" @click.native.stop="goBack" size="24"></u-icon>
				<u-icon name="plus" size="24" @click.native.stop="goApply" v-if="$isPermission('familyMembers_add')"></u-icon>
			</view>

		</u-navbar>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN -->
		<u-navbar :placeholder="true" :autoBack="true">
			<view class="u-nav-slot" slot="center">
				<view class="" @click="show = true" v-if="show">
					<zb-dropdown-menu :zIndex="89" @toggleItem="toggleItem(2)" ref="dropdown1" active-color="#1cb8a7">
						<zb-dropdown-item :options="option" v-model="value4" @change="change1"
							name="first"></zb-dropdown-item>
					</zb-dropdown-menu>
				</view>
				<view class="" v-else style="font-weight: 700;font-size: 16px;">
					宝宝家属
				</view>
			</view>
			<view class="u-nav-slot" slot="right">
				<u-icon name="plus" size="24" @click.native.stop="goApply" v-if="$isPermission('familyMembers_add')"></u-icon>
			</view>
		</u-navbar>
		<!-- #endif -->
		<view class="content">
			<view class="card" v-for="(item,i) in parentlist" @click="goDetail(item)" :key="i">
				<view class="top">
					<view class="left">家长姓名：</view>
					<view class="right">{{item.userName}}</view>
				</view>
				<view class="top">
					<view class="left">亲属关系：</view>
					<view class="right">{{item.relationshipName}}</view>
				</view>
				<view class="top">
					<view class="left">家长手机：</view>
					<view class="right">{{item.userMobile}}</view>
				</view>
				<u-line></u-line>
				<view class="top" style="margin-top: 25rpx;">
					<view class="left text-green" @click="editContacts(i)">编辑</view>
					<view class="right text-bold" v-if="item.firstContacts=='1'">
						第一联系人
					</view>
					<view v-else class="right text-red"  @click="deleteContact(i)">
						删除
					</view>
				</view>
				<!-- <view class="" >
					<u-row justify="space-between" style="margin-top: 20rpx;">
						<u-col span="6">
							<view class="demo-layout text-center" @click="editContacts(i)">
								编辑
							</view>
						</u-col>
						<u-col span="6" v-if="item.firstContacts=='1'">
							<view class="demo-layout text-red text-center" @click="deleteContact(i)">
								删除
							</view>
						</u-col>
						<u-col span="6" v-if="item.firstContacts=='1'">
							<view class="demo-layout text-red text-center" @click="deleteContact(i)">
								删除
							</view>
						</u-col>
						
						<u-col span="5">
							<view class="demo-layout text-center" v-if="item.firstContacts=='1'">
								第一联系人
							</view>
							<view class="demo-layout text-center text-red" v-if="item.firstContacts=='0'" @click="deleteContact(i)">
								删除
							</view>
						</u-col>
						<u-col span="3">
							<view class="demo-layout text-center" @click="resetPassword(i)">
								重置密码
							</view>
						</u-col>
					</u-row>
				</view> -->
			</view>
		</view>
		<!-- 提示窗示例 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="取消" confirmText="确定" title="提示" content='确认重置当前账号密码？重置后，密码为APP默认登录密码。可在【园区设置>系统参数设置】中查看"APP默认登录密码"' @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<!-- <button v-if="!show"  @click="goApply" class="btn" ></button> -->
	</view>
</template>

<script>
	// import {parentEntrustPage} from '@/api/entrust.js'
	import {info,updateContacts,resetPwd,del} from '@/api/parents.js';
	export default {
		data() {
			return {
				value4: 0,
				option: [{
						text: '大一班',
						value: 0
					},
					{
						text: '大二班',
						value: 1
					},
					{
						text: '小一班',
						value: 2
					}
				],
				parentlist: [],
				show: false,
				msgType:"warn",
			};
		},
		onLoad() {
			if (this.show) {

			} else {

			}
			// this.parentEntrustPage()
			// console.log('frfr===========',this.$util.getChild().userId);
			this.studentId = this.$util.getChild().userId;
			this.info(this.studentId);
			// this.info()
		},
		methods: {
			info(id){
				info(id).then(
					res => {
						if (res.data.code=='0') {
							this.parentlist = res.data.data;
							let list = this.$util.getdictionary('幼儿身份证件类型');
							let list1 = this.$util.getdictionary('亲属关系');
							let list2 = this.$util.getdictionary('民族');
							let list3 = this.$util.getdictionary('教育程度');
							this.parentlist.map(hj=>{
								list.forEach(mn=>{
								 if(hj.userIdCardType){
									if(hj.userIdCardType == mn.value){
										hj.userIdCardTypeName = mn.name
									} 
								 }else{
									 hj.userIdCardTypeName='-'
								 }
								})
								
								list1.forEach(mn1=>{
									if(hj.relationship == mn1.value){
										hj.relationshipName = mn1.name
									}
								})
								
								list2.forEach(mn2=>{
								 if(hj.userNation){
									if(hj.userNation == mn2.value){
										hj.userNationName = mn2.name
									} 
								 }else{
									 hj.userNationName='-'
								 }
								})
								
								list3.forEach(mn3=>{
								 if(hj.userEducation){
									if(hj.userEducation == mn3.value){
										hj.userEducationName = mn3.name
									} 
								 }else{
									 hj.userEducationName='-'
								 }
								})
								
								return hj
							})
							console.log('')
						}else{
							this.$api.showTip(res.data.msg)
						}
					},
					err => {
						this.$api.showTip('服务器错误')
					}
				);		
			},
			
			// 设为第一联系人
			firstContact(index){
			this.parentlist.forEach(nm=>{
				nm.firstContacts = '0'
			})
			this.parentlist[index].firstContacts = '1';
				updateContacts(this.parentlist[index]).then(
					res => {
						if (res.data.code=='0') {
							this.$api.showTip('修改成功')
						}else{
							this.$api.showTip(res.data.msg)
						}
					},
					err => {
						this.$api.showTip('服务器错误')
					}
				);
			},
			// 重置密码
			resetPassword(index){
				this.$refs.alertDialog.open();
				this.userId = this.parentlist[index].userId;
				
			},
			
			dialogClose(){
			  console.log('点击关闭')
			},
			dialogConfirm() {
				uni.showLoading({
					title: '修改中...',
					mask: true
				})
				
				resetPwd(this.userId).then(
					res => {
						if (res.data.code=='0') {
							this.$api.showTip("操作成功", () => {
								// this.$refs.tagsPopup.close();
								// this.tablelist();
								this.$refs.alertDialog.close()
							});
						}else{
							this.$api.showTip(res.data.msg)
						}
						uni.hideLoading();
					},
					err => {uni.hideLoading();}
				);		
				
				
			},
			// 删除
			deleteContact(index) {
				uni.showModal({
				    title: '提示',
				    content: '确认删除该记录？',
				    success: (res) => {
				        res.confirm && this.gradeDelete(index);
						
						
				    }
				})
			},
			gradeDelete(index) {
			  del(this.parentlist[index]).then(
				res => {
					if (res.data.code=='0') {
						this.$api.showTip("操作成功", () => {
							this.info(this.studentId);
						});
					}else{
						this.$api.showTip(res.data.msg)
					}
					uni.hideLoading();
				},
				err => {uni.hideLoading();}
			  );		
			},
			
			editContacts(index){
				this.parentlist[index].edit = true;
				uni.navigateTo({
					url:'add?editlist=' + JSON.stringify(this.parentlist[index])
				})
			},
			
			goBack(){
				uni.navigateBack()
			},
			goApply() {
				uni.navigateTo({
					url: 'add'
				})
			},
			change1(val) {
				console.log('===========', val, this.value1);
			},
			goDetail(it) {
				uni.navigateTo({
					url: 'detail?info='+JSON.stringify(it)
				})
			},
			toggleItem(val) {
				if (val === 1) {
					this.$refs.dropdown1.close();
				} else {
					// this.$refs.dropdown.close()
				}
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.u-nav-slot {
		display: flex;
	}

	/deep/.uicon-plus {
		margin-left: 90rpx;
	}

	.state {
		position: absolute;
		right: 0;
		top: 0;
		width: 80rpx;
		height: 40rpx;
		text-align: center;
		background-color: #20C3B2;
		color: #fff;
		font-size: 22rpx;
		font-weight: bold;
		line-height: 40rpx;
		border-bottom-left-radius: 16rpx;
		border-top-right-radius: 16rpx;
	}

	/deep/.zb-dropdown-menu__bar {
		box-shadow: none;
		height: 100%;
	}

	/deep/.zb-dropdown-item {
		top: 44px !important;
	}

	// /deep/.u-button{
	// 	position: fixed;
	// 	bottom: 100rpx;
	// 	left: 30%;
	// 	width: 200rpx;
	// 	background-color: $uni-theme-color;
	// 	color: #fff;
	// 	margin-top: 30rpx;
	// }
	.content {
		
		padding: 20rpx;

		.card {
			position: relative;
			width: 100%;
			padding: 14rpx 25rpx;
			background-color: #fff;
			border-radius: 14rpx;
			box-sizing: border-box;
			margin-bottom: 20rpx;

			.top {
				display: flex;
				justify-content: space-between;
				margin-bottom: 15rpx;

				.left {
					// font-weight: 700;
					font-size: 28rpx;
				}
			}
		}
	}

	.fixBtn {}
</style>